package com.itlong.cloud.iot.alibaba.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.itlong.cloud.PlatformConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 *  阿里IoT通信环境初始化
 * @createDate 2018-03-14
 */
@Component
public class AliIotInitClient {
    private static final Logger LOG = LoggerFactory.getLogger(AliIotInitClient.class);

    @Value("${" + PlatformConstants.IOT_SERVICE_URL + "}")
    private  String iotServiceUrl; //华东1服务接入地址
    @Value("${" + PlatformConstants.IOT_ACCESS_KEY_ID + "}")
    private  String iotAccessKeyId;  //阿里云颁发给用户的访问服务所用的密钥ID
    @Value("${" + PlatformConstants.IOT_ACCESS_KEY_SECRET + "}")
    private  String iotAccessKeySecret;
    @Value("${" + PlatformConstants.IOT_REGION_ID + "}")
    private  String iotRegionId;
    @Value("${"+PlatformConstants.IOT_DEFAULT_PRODUCT_KEY+"}")
    private  String iotDefaultProductKey;


    private static volatile DefaultAcsClient client = null;
    private AliIotInitClient(){}

    /**
     * <desc>
     *      获取阿里云iot通信环境client
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @createDate 2018/03/17
     */
    public  DefaultAcsClient getClient() {
        try {
            if (client == null){
                synchronized (DefaultAcsClient.class){
                    if (client == null){
                        IClientProfile profile = DefaultProfile.getProfile(iotRegionId, iotAccessKeyId, iotAccessKeySecret);
                        DefaultProfile.addEndpoint(iotRegionId,iotRegionId,iotDefaultProductKey,iotServiceUrl);
                        // 初始化client
                        client = new DefaultAcsClient(profile);
                    }
                }
            }
            return client;
        }catch (Exception e){
            LOG.error("DefaultAcsClient.getClient执行失败：e:" + e.getMessage());
            return client;
        }

    }








}
